package thread.waitandnotify;

/**
 * @author liweimin
 * @date 2021/4/30 16:41
 * @since
 */
public class Test6 {
    public static final Object lock = new Object();
    public volatile static boolean flag = true;

    public static void main(String[] args) {
        Thread thread1 = new Thread(() -> {
            synchronized (lock) {
                while (flag)//如果是第一个运行的线程就是等待
                {
                    try {
                        System.out.println(Thread.currentThread().getName() + "===========wait begin===========");

                        lock.wait();
                        System.out.println(Thread.currentThread().getName() + "===========wait end===========");

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        Thread thread2 = new Thread(() -> {
            synchronized (lock) {
                System.out.println(Thread.currentThread().getName() + "===========notify begin===========");
                lock.notify();
                System.out.println(Thread.currentThread().getName() + "===========notify end===========");
                flag = false;
            }
        });

        /*thread2 线程先唤醒 thread1线程就不等待了*/

        thread1.start();
        thread2.start();
//        thread2.start();
//        thread1.start();
    }
}
